package csd.wyaiyr.com.config;

import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import java.util.Date;

public class BaseResult<T> implements Serializable {

//   响应状态码

    @Schema(name = "code",description = "响应状态码")
    private int code;

//    是否成功

    @Schema(name = "success",description = "是否成功")
    private boolean success = false;


//     接口返回描述信息

    @Schema(name = "msg",description = "接口返回描述信息")
    private String msg;

//    接口返回数据

    @Schema(name = "data",description = "接口返回数据")
    private T data;

//   响应时间戳

    @Schema(name = "timestamp",description = "响应时间戳")
    private String timestamp;

    public BaseResult() {
    }

    public BaseResult(String msg) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.timestamp = timestamp;
    }


    public BaseResult(int code, boolean success, String msg, String timestamp) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.timestamp = timestamp;
    }


    public BaseResult(int code, boolean success, String msg, T data, String timestamp) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.data = data;
        this.timestamp = timestamp;
    }


    public BaseResult(int code, boolean success, String msg, T data, String timestamp, PageInfoResult page) {
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.data = data;
        this.timestamp = timestamp;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "BaseResult{" +
                "code=" + code +
                ", success=" + success +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp='" + timestamp + '\'' +
                '}';
    }

    public static BaseResult ok(){
        BaseResult baseResult = new BaseResult(200,true,"成功", new Date().getTime()+"");
        return baseResult;
    }

    public static BaseResult ok(String msg){
        BaseResult baseResult = new BaseResult(200,true,msg, new Date().getTime()+"");
        return baseResult;
    }

    public static BaseResult ok(Object data){
        BaseResult baseResult = new BaseResult(200,true,"成功", data, new Date().getTime()+"");
        return baseResult;
    }

    public static BaseResult ok(String msg,Object data){
        BaseResult baseResult = new BaseResult(200,true,msg, data, new Date().getTime()+"");
        return baseResult;
    }

    // 用于前端判断 状态码200 成功标志为false
    public static BaseResult okError(String msg){
        BaseResult baseResult = new BaseResult();
        baseResult.setSuccess(false);
        baseResult.setCode(200);
        baseResult.setMsg(msg);
        baseResult.setTimestamp(new Date().getTime()+"");
        return baseResult;
    }



    public static BaseResult error(){
        BaseResult baseResult = new BaseResult(500,false,"失败", new Date().getTime()+"");
        return baseResult;
    }

    public static BaseResult error(Object data){
        BaseResult baseResult = new BaseResult(500,false,"失败", data, new Date().getTime()+"");
        return baseResult;
    }

    public static BaseResult error(int code , String errMsg){
        BaseResult baseResult = new BaseResult(code,false,errMsg,new Date().getTime()+"");
        return baseResult;
    }


    public static BaseResult error(int code , String errMsg, Object data){
        BaseResult baseResult = new BaseResult(code,false,errMsg, data, new Date().getTime()+"");
        return baseResult;
    }



}
